package com.rfid.util;

import com.greendao.gen.DaoSession;
import com.greendao.gen.DeletedDao;
import com.greendao.gen.MachineDao;
import com.greendao.gen.OperationDao;
import com.greendao.gen.RfidDao;
import com.rfid.db.bean.Deleted;
import com.rfid.db.bean.Gather;
import com.rfid.db.bean.Machine;
import com.rfid.db.bean.Operation;
import com.rfid.db.bean.Rfid;
import com.rfid.db.bean.Shunjia;
import com.rfid.db.bean.ShunjiaLevel;
import com.rfid.db.bean.User;
import com.rfid.rls_psw.App;

import org.greenrobot.greendao.query.WhereCondition;

import java.util.List;

public class DBManager {


    public static List<Rfid> queryRfidsByShelf(String shelf) {
        System.out.println("queryRfidsByShelf shelf = " + shelf);
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition condition = daoSession.getRfidDao().queryBuilder().and(RfidDao.Properties.Shelf.eq(shelf),RfidDao.Properties.Deleted.eq(0));
        return daoSession.getRfidDao().queryBuilder().where(condition).build().list();
//        return daoSession.getRfidDao().queryBuilder().build().list();
    }

    public static Rfid queryRfidByBarcode(String barcode) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getRfidDao().queryBuilder().where(RfidDao.Properties.Barcode.eq(barcode)).build().unique();
    }

    public static Rfid queryRfidByUid(String epc) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getRfidDao().queryBuilder().where(daoSession.getRfidDao().queryBuilder().and(RfidDao.Properties.Uid.eq(epc),RfidDao.Properties.Deleted.eq(0))).build().unique();
    }

    public static void updateRfids(List<Rfid> rfids) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        daoSession.getRfidDao().updateInTx(rfids);
    }

    public static Rfid queryRfidById(long id) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getRfidDao().queryBuilder().where(RfidDao.Properties.Id.eq(id)).build().unique();
    }

    public static long queryUnUploadCount() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getOperationDao().queryBuilder().where(OperationDao.Properties.Upload.eq(false)).count();
    }

    public static List<Rfid> queryRfidsByBookName(String name) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition condition = daoSession.getRfidDao().queryBuilder().and(RfidDao.Properties.Deleted.eq(0),RfidDao.Properties.BookName.like("%" + name + "%"));
        return daoSession.getRfidDao().queryBuilder().where(condition).build().list();
    }

    public static List<Rfid> queryRfidsByBookNameOrBarCode(String name) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition condition = daoSession.getRfidDao().queryBuilder().or(RfidDao.Properties.BookName.like("%" + name + "%"),RfidDao.Properties.Barcode.like(name + "%"));
        condition = daoSession.getRfidDao().queryBuilder().and(RfidDao.Properties.Deleted.eq(0),condition);
        return daoSession.getRfidDao().queryBuilder().where(condition).build().list();
    }

    public static Rfid queryRfidByRIndex(String rindex) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getRfidDao().queryBuilder().where(RfidDao.Properties.Rindex.like(rindex + "%")).build().unique();
    }

    public static void updateRfid(Rfid rfid) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        daoSession.getRfidDao().update(rfid);
        daoSession.clear();
    }
//----------------------------------------------------------
    public static long insertMachine(Machine machine) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        long count = daoSession.getMachineDao().queryBuilder().where(MachineDao.Properties.Epc.eq(machine.getEpc())).count();
        if(count > 0) {
            return -1;
        }
        return daoSession.getMachineDao().insert(machine);
    }

    public static List<Machine> queryMachines() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getMachineDao().queryBuilder().build().list();
    }

    public static void updateMachine(Machine machine) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        daoSession.getMachineDao().update(machine);
    }

    public static List<Machine> queryMachinesByDimInfo(String keyword) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getMachineDao().queryBuilder().where(daoSession.getMachineDao().queryBuilder().or(MachineDao.Properties.Epc.like("%"+keyword+"%"),MachineDao.Properties.Info.like("%"+keyword+"%"))).build().list();
    }

    public static long queryMachineCount() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getMachineDao().queryBuilder().count();
    }
    //----------------------------------------------------

    public static void insertOperation(Operation operation) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        daoSession.getOperationDao().insertInTx(operation);
    }

    public static List<Rfid> queryOperationRfids(String name,int operation,String start,String end) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition.StringCondition condition = new WhereCondition.StringCondition("id in (select rfid from operation where user = '" + name + "' and operation = " + operation + " and date Between '" + start + "' and '" + end + "')");
        return daoSession.getRfidDao().queryBuilder().where(condition).build().list();
    }


    public static List<Rfid> queryOperationRfids(String name,String start,String end) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition.StringCondition condition = new WhereCondition.StringCondition("id in (select rfid from operation where user = '" + name + "'" + " and date Between '" + start + "' and '" + end + "')");
        return daoSession.getRfidDao().queryBuilder().where(condition).build().list();
    }

    public static List<Operation> queryOperation(String name,int operation,String start,String end) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getOperationDao().queryBuilder().where(daoSession.getOperationDao().queryBuilder().and(OperationDao.Properties.User.eq(name),OperationDao.Properties.Operation.eq(operation),OperationDao.Properties.Date.between(start,end))).build().list();
    }

    public static List<Operation> queryOperation(String name,String start,String end) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getOperationDao().queryBuilder().where(daoSession.getOperationDao().queryBuilder().and(OperationDao.Properties.User.eq(name),OperationDao.Properties.Date.between(start,end))).build().list();
    }

    public static List<Operation> queryUnuploadOperation() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getOperationDao().queryBuilder().where(OperationDao.Properties.Upload.eq(0)).build().list();
    }

    public static List<Operation> queryUnuploadOperation(int type) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition condition = daoSession.getOperationDao().queryBuilder().and(OperationDao.Properties.Upload.eq(0),OperationDao.Properties.Operation.eq(type));
        return daoSession.getOperationDao().queryBuilder().where(condition).build().list();
    }

    public static List<Operation> queryUnuploadLocateOperation() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition condition = daoSession.getOperationDao().queryBuilder().and(OperationDao.Properties.Upload.eq(0),OperationDao.Properties.Operation.eq(Operation.OPERATE_LOCATE));
        return daoSession.getOperationDao().queryBuilder().where(condition).build().list();
    }

    public static List<Gather> queryGathers() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getGatherDao().queryBuilder().build().list();
    }

    public static List<Operation> queryUnuploadCheckOperation() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition condition = daoSession.getOperationDao().queryBuilder().and(OperationDao.Properties.Upload.eq(0),OperationDao.Properties.Operation.eq(Operation.OPERATE_CHECK));
        return daoSession.getOperationDao().queryBuilder().where(condition).build().list();
    }

    public static List<Operation> queryUnuploadDeleteOperation() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition condition = daoSession.getOperationDao().queryBuilder().and(OperationDao.Properties.Upload.eq(0),OperationDao.Properties.Operation.eq(Operation.OPERATE_DELETE));
        return daoSession.getOperationDao().queryBuilder().where(condition).build().list();
    }
    public static List<Operation> queryUnuploadShangjiaOperation() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition condition = daoSession.getOperationDao().queryBuilder().and(OperationDao.Properties.Upload.eq(0),OperationDao.Properties.Operation.eq(Operation.OPERATE_SHANGJIA));
        return daoSession.getOperationDao().queryBuilder().where(condition).build().list();
    }

    public static List<User> queryUsers() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getUserDao().queryBuilder().list();
    }

    public static List<Rfid> queryRfidsByDeletedTable() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition.StringCondition condition = new WhereCondition.StringCondition("uid in (select uid from deleted)");
        return daoSession.getRfidDao().queryBuilder().where(condition).build().list();
    }

    public static Deleted queryDeleted(String uid) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getDeletedDao().queryBuilder().where(DeletedDao.Properties.Uid.eq(uid)).build().unique();
    }

    public static List<Rfid> queryLocalDeletedRfids() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getRfidDao().queryBuilder().where(RfidDao.Properties.Deleted.eq("1")).build().list();
    }

    public static void deleteDeleted(Deleted deleted) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        daoSession.getDeletedDao().delete(deleted);
    }

    public static List<Rfid> queryRfidsByIndex(String start,String end) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        WhereCondition condition = daoSession.getRfidDao().queryBuilder().and(RfidDao.Properties.Rindex.ge(start),RfidDao.Properties.Rindex.lt(end));
        return daoSession.getRfidDao().queryBuilder().where(condition).build().list();
    }

    public static List<Rfid> queryRfidsLikeIndex(String like) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getRfidDao().queryBuilder().where(RfidDao.Properties.Rindex.like(like + "%")).build().list();
    }

    public static void updateOperate(List<Operation> operations) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        daoSession.getOperationDao().updateInTx(operations);
    }

    public static List<Shunjia> queryShunjias() {
        DaoSession daoSession = App.getInstance().getDaoSession();
        return daoSession.getShunjiaDao().queryBuilder().build().list();
    }

    public static void addShunjiaLevel(ShunjiaLevel level) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        daoSession.getShunjiaLevelDao().insert(level);
    }

    public static void addGather(Gather gather) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        daoSession.getGatherDao().insert(gather);
    }

    public static void insertUser(User user) {
        DaoSession daoSession = App.getInstance().getDaoSession();
        daoSession.getUserDao().insert(user);
    }
}
